package com.sari.common.web;

import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * 获取所有的controller注解。这个可以考虑做成自动化自动写入菜单,就不再需要手工维护菜单
 * Created by Hizhengfu on 2014/10/23.
 */
@Controller
@RequestMapping(value = "sys/scan")
public class ScanApi {

    @Autowired
    private RequestMappingHandlerMapping rmhp;

    @ResponseBody
    @RequestMapping("")
    public Object getUrlMapping(HttpServletRequest request) {
        List result = Lists.newArrayList();
        Map map = rmhp.getHandlerMethods();
        Iterator<?> iterator = map.entrySet().iterator();
        while (iterator.hasNext()) {
            result.add(iterator.next());
        }
        return result;
    }

    public WebApplicationContext getWebApplicationContext(ServletContext sc) {
        return WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
    }
}
